package com.example.hehecat.yuejian1.ui.activity;



import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.widget.FrameLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.example.hehecat.yuejian1.R;
import com.example.hehecat.yuejian1.ui.fragment.ActivityListFragment;
import com.example.hehecat.yuejian1.ui.fragment.ClubFragment;
import com.example.hehecat.yuejian1.ui.fragment.MatchFragment;
import com.example.hehecat.yuejian1.ui.fragment.MineFragment;
import com.example.hehecat.yuejian1.ui.fragment.PictureFragment;
import com.example.hehecat.yuejian1.ui.fragment.TrainFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.id_content)
    FrameLayout idContent;
    @BindView(R.id.bottom_navigation_bar)
    BottomNavigationBar bottomNavigationBar;
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    private List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        setSupportActionBar(toolbar);
        // 主标题
        toolbar.setTitle("活动");
        //设置toolbar
        setSupportActionBar(toolbar);

        fragments = new ArrayList<>();
        initBottomBar();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search, menu);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.ab_search).getActionView();

        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        return true;
    }

    /**
     * 设置默认显示的fragment
     */
    private void setDefaultFragment() {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.id_content, ActivityListFragment.newInstance("首页"));
        transaction.commit();
    }

    /**
     * 获取fragment
     *
     * @return fragment列表
     */
    private List<Fragment> getFragments() {

        List<Fragment> fragments = new ArrayList<>();
        fragments.add(ActivityListFragment.newInstance("首页"));
        fragments.add(ClubFragment.newInstance("俱乐部"));
        fragments.add(PictureFragment.newInstance("晒图"));
        fragments.add(MatchFragment.newInstance("比赛"));
        fragments.add(TrainFragment.newInstance("训练"));
        fragments.add(MineFragment.newInstance("我的"));

        return fragments;
    }


    private void initBottomBar() {

        fragments = getFragments();
        setDefaultFragment();

        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.bt_activity, "活动"))
                .addItem(new BottomNavigationItem(R.drawable.bt_club, "俱乐部"))
                .addItem(new BottomNavigationItem(R.drawable.bt_picture, "晒图"))
                .addItem(new BottomNavigationItem(R.drawable.bt_match, "比赛"))
                .addItem(new BottomNavigationItem(R.drawable.bt_train, "训练"))
                .addItem(new BottomNavigationItem(R.drawable.bt_mine, "我的"))
                .initialise();

        bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                if (fragments != null) {
                    if (position < fragments.size()) {
                        FragmentManager fm = getSupportFragmentManager();
                        FragmentTransaction ft = fm.beginTransaction();
                        Fragment fragment = fragments.get(position);
                        ft.replace(R.id.id_content, fragment);
                        ft.commit();

                        String title = fragment.getArguments().getString("name");
                        toolbar.setTitle(title);


                    }
                }
            }

            @Override
            public void onTabUnselected(int position) {
            }

            @Override
            public void onTabReselected(int position) {
            }
        });

    }


}
